<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams">
      <el-form-item label="样本复份编号" prop="sampBackupCode">
        <el-input
          v-model="queryParams.sampBackupCode"
          clearable
          placeholder="请输入样本复份编号"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="样本类型" prop="sampTypeId">
        <treeselect v-model="queryParams.sampTypeId" :normalizer="normalizerSampType" :options="sampleTypeOptions"
                    placeholder="请选择样本类型" style="width: 214px"
        />
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['biobank:sampleTrash:edit']"
          icon="el-icon-refresh-right"
          plain
          size="mini"
          type="warning"
          @click="handleReduce"
        >批量还原
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sampleTrashList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column align="center" label="样本复份编号" min-width="100" prop="sampBackupCode"/>
      <el-table-column :formatter="sampTypeFormat" align="center" label="样本类型" prop="sampTypeId"/>
      <el-table-column align="center" label="删除原因" prop="remark" width="600"/>
      <el-table-column align="center" label="操作人" prop="createBy"/>
      <el-table-column align="center" label="操作时间" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['biobank:sampleTrash:edit']"
            icon="el-icon-refresh-right"
            size="mini"
            type="text"
            @click="handleReduce(scope.row)"
          >还原
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listSampleTrash, reduceSampleTrash } from '@/api/biobank/sampleTrash'
import { listSampleType } from '@/api/biobank/sampleType'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  name: 'SampleTrash',
  components: {
    Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      selectTrashs: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 样本回收站 表格数据
      sampleTrashList: [],
      // 样本类型
      sampleTypes: [],
      // 样本类型 树选项
      sampleTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sampBackupId: undefined,
        sampBackupCode: undefined,
        sampTypeId: undefined
      }
    }
  },
  created() {
    this.getSampleTypeList()
    this.getList()
  },
  methods: {
    /** 查询样本回收站 列表 */
    getList() {
      this.loading = true
      listSampleTrash(this.queryParams).then(response => {
        this.sampleTrashList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 查询样本类型 列表 */
    getSampleTypeList() {
      listSampleType().then(response => {
        this.sampleTypes = response.data
        this.sampleTypeOptions = this.handleTree(response.data, 'sampTypeId', 'parentId')
      })
    },
    /** 转换样本类型 数据结构 */
    normalizerSampType(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.sampTypeId,
        label: node.sampTypeName,
        children: node.children
      }
    },
    sampTypeFormat(row, column) {
      var actions = []
      Object.keys(this.sampleTypes).some((key) => {
        if (this.sampleTypes[key].sampTypeId == row.sampTypeId) {
          actions.push(this.sampleTypes[key].sampTypeName)
          return true
        }
      })
      return actions.join('')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selectTrashs = selection
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    // 样本还原
    handleReduce(row) {
      let sampTrashs
      if (row.sampTrashId) {
        sampTrashs = [row]
      } else {
        sampTrashs = this.selectTrashs
      }
      this.$confirm('是否确认还原样本复份', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return reduceSampleTrash(sampTrashs)
      }).then(() => {
        this.getList()
        this.msgSuccess('还原成功，请进入样本入库界面重新入库')
      })
    }
  }
}
</script>
